package in.muton.edushare.newentities;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.BatchDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.Users;

public class BatchUserInviter extends UserInviter {
	private static final long serialVersionUID = 3298124573612580238L;
	IModel<Batches> batch;
	Users creator;
	ORMInterface ormInterface;

	public BatchUserInviter(ORMInterface ormInterface, IModel<Batches> b) {
		super(ormInterface);
		this.ormInterface = ormInterface;
		batch = b;
		creator = EduShareSession.get().getLoggedInUser();
	}

	public List<String> addUsersToBatch(String fieldInput) {
		String[] users = breakString(fieldInput);
		List<String> statusString = new ArrayList<String>();
		for (String u : users) {
			addDetailedStudent(u, "", "", "");
		}
		return statusString;
	}

	public List<String> addBulkUsersFromExcelSheet(List<String[]> excelInput) {
		List<String> statusString = new ArrayList<String>();
		for (int i = 0; i < excelInput.size(); i++) {
			String[] row = excelInput.get(i);
			String emailId = row[0];
			String rollNum = row[1];
			String firstName = row[2];
			String lastName = row[3];
			statusString.add(addDetailedStudent(emailId, rollNum, firstName,
					lastName));
		}
		return statusString;
	}

	public String addDetailedStudent(String emailId, String rollNum,
			String firstName, String lastName) {
		DeptUserCreator duc = getDUC();
		BatchDAO bDAO = new BatchDAO(ormInterface);
		Departments dept = batch.getObject().getCurricula().getDepartment();
		Universities univ = new DepartmentsDAO(ormInterface).getParent(dept);
		Users userVO = getUserByString(emailId);
		if (userVO != null) {
			duc.addUsersToDept(emailId);
			bDAO.addUserFB(batch.getObject(), userVO);
		} else {
			if (isEmailId(emailId)) {
				duc.addUsersToDept(emailId);
				userVO = getUserByString(emailId);
				if (userVO != null) {
					bDAO.addUserFB(batch.getObject(), userVO);
				} else {
					return "Unable to invite user [" + emailId + "," + rollNum
							+ "," + firstName + "," + lastName + "]";
				}
			} else {
				return "Unable to invite user [" + emailId + "," + rollNum
						+ "," + firstName + "," + lastName + "]";
			}
		}
		new UsersDAO(ormInterface).updateUserDetails(userVO, firstName,
				lastName);
		return new UserRolesDAO(ormInterface).addStudentToUniv(univ, userVO,
				creator, rollNum);
	}

	private DeptUserCreator getDUC() {
		return new DeptUserCreator(EduShareSession.get()
				.getSessionORMInterface(), new CayenneModel<Departments>(batch
				.getObject().getCurricula().getDepartment()),
				UserDeptLink.STUDENT);
	}
}
